home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / PAT202SR.ZIP / src / patch / inp.c < prev    next >
C/C++ Source or Header  |  1993-11-28  |  9KB  |  323 lines

  1. /* $Header: inp.c,v 2.0.1.1 88/06/03 15:06:13 lwall Locked $
  2.  *
  3.  * $Log:    inp.c,v $
  4.  * Revision 2.0.1.1  88/06/03  15:06:13  lwall
  5.  * patch10: made a little smarter about sccs files
  6.  * 
  7.  * Revision 2.0  86/09/17  15:37:02  lwall
  8.  * Baseline for netwide release.
  9.  * 
  10.  */
  11.  
  12. #include "EXTERN.h"
  13. #include "common.h"
  14. #include "util.h"
  15. #include "pch.h"
  16. #include "INTERN.h"
  17. #include "inp.h"
  18.  
  19. /* Input-file-with-indexable-lines abstract type */
  20.  
  21. static long i_size;            /* size of the input file */
  22. static char *i_womp;            /* plan a buffer for entire file */
  23. static char **i_ptr;            /* pointers to lines in i_womp */
  24.  
  25. static int tifd = -1;            /* plan b virtual string array */
  26. static char *tibuf[2];            /* plan b buffers */
  27. static LINENUM tiline[2] = {-1, -1};    /* 1st line in each buffer */
  28. static LINENUM lines_per_buf;        /* how many lines per buffer */
  29. static int tireclen;            /* length of records in tmp file */
  30.  
  31. /* New patch--prepare to edit another file. */
  32.  
  33. void
  34. re_input()
  35. {
  36.     if (using_plan_a) {
  37.     i_size = 0;
  38. #ifndef lint
  39.     if (i_ptr != Null(char**))
  40.         free((char *)i_ptr);
  41. #endif
  42.     if (i_womp != Nullch)
  43.         free(i_womp);
  44.     i_womp = Nullch;
  45.     i_ptr = Null(char **);
  46.     }
  47.     else {
  48.     using_plan_a = TRUE;        /* maybe the next one is smaller */
  49.     Close(tifd);
  50.     tifd = -1;
  51.     free(tibuf[0]);
  52.     free(tibuf[1]);
  53.     tibuf[0] = tibuf[1] = Nullch;
  54.     tiline[0] = tiline[1] = -1;
  55.     tireclen = 0;
  56.     }
  57. }
  58.  
  59. /* Constuct the line index, somehow or other. */
  60.   
  61. void
  62. scan_input(filename)
  63. char *filename;
  64. {
  65.     if (!plan_a(filename))
  66.     plan_b(filename);
  67.     if (verbose) {
  68.     say3("Patching file %s using Plan %s...\n", filename,
  69.       (using_plan_a ? "A" : "B") );
  70.     }
  71. }
  72.  
  73. /* Try keeping everything in memory. */
  74.  
  75. bool
  76. plan_a(filename)
  77. char *filename;
  78. {
  79.     int ifd;
  80.     Reg1 char *s;
  81.     Reg2 LINENUM iline;
  82.  
  83.     if (ok_to_create_file && stat(filename, &filestat) < 0) {
  84.     if (verbose)
  85.         say2("(Creating file %s...)\n",filename);
  86.     makedirs(filename, TRUE);
  87.     close(creat(filename, 0666));
  88.     }
  89.     if (stat(filename, &filestat) < 0) {
  90.     Sprintf(buf, "RCS/%s%s", filename, RCSSUFFIX);
  91.     if (stat(buf, &filestat) >= 0 || stat(buf+4, &filestat) >= 0) {
  92.         Sprintf(buf, CHECKOUT, filename);
  93.         if (verbose)
  94.         say2("Can't find %s--attempting to check it out from RCS.\n",
  95.             filename);
  96.         if (system(buf) || stat(filename, &filestat))
  97.         fatal2("Can't check out %s.\n", filename);
  98.     }
  99.     else {
  100.         Sprintf(buf+20, "SCCS/%s%s", SCCSPREFIX, filename);
  101.         if (stat(s=buf+20, &filestat) >= 0 ||
  102.           stat(s=buf+25, &filestat) >= 0) {
  103.         Sprintf(buf, GET, s);
  104.         if (verbose)
  105.             say2("Can't find %s--attempting to get it from SCCS.\n",
  106.             filename);
  107.         if (system(buf) || stat(filename, &filestat))
  108.             fatal2("Can't get %s.\n", filename);
  109.         }
  110.         else
  111.         fatal2("Can't find %s.\n", filename);
  112.     }
  113.     }
  114.     filemode = filestat.st_mode;
  115.     if ((filemode & S_IFMT) & ~S_IFREG)
  116.     fatal2("%s is not a normal file--can't patch.\n", filename);
  117.     i_size = filestat.st_size;
  118.     if (out_of_mem) {
  119.     set_hunkmax();        /* make sure dynamic arrays are allocated */
  120.     out_of_mem = FALSE;
  121.     return FALSE;            /* force plan b because plan a bombed */
  122.     }
  123. #ifdef lint
  124.     i_womp = Nullch;
  125. #else
  126.     i_womp = malloc((MEM)(i_size+2));    /* lint says this may alloc less than */
  127.                     /* i_size, but that's okay, I think. */
  128. #endif
  129.     if (i_womp == Nullch)
  130.     return FALSE;
  131.     if ((ifd = open(filename, 0)) < 0)
  132.     fatal2("Can't open file %s\n", filename);
  133. #ifndef lint
  134.     if (read(ifd, i_womp, (int)i_size) != i_size) {
  135. #ifndef __MSDOS__
  136. /* text files always do this */
  137.     Close(ifd);    /* probably means i_size > 15 or 16 bits worth */
  138.     free(i_womp);    /* at this point it doesn't matter if i_womp was */
  139.     return FALSE;    /*   undersized. */
  140. #endif
  141.     }
  142. #endif
  143.     Close(ifd);
  144.     if (i_size && i_womp[i_size-1] != '\n')
  145.     i_womp[i_size++] = '\n';
  146.     i_womp[i_size] = '\0';
  147.  
  148.     /* count the lines in the buffer so we know how many pointers we need */
  149.  
  150.     iline = 0;
  151.     for (s=i_womp; *s; s++) {
  152.     if (*s == '\n')
  153.         iline++;
  154.     }
  155. #ifdef lint
  156.     i_ptr = Null(char**);
  157. #else
  158.     i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
  159. #endif
  160.     if (i_ptr == Null(char **)) {    /* shucks, it was a near thing */
  161.     free((char *)i_womp);
  162.     return FALSE;
  163.     }
  164.     
  165.     /* now scan the buffer and build pointer array */
  166.  
  167.     iline = 1;
  168.     i_ptr[iline] = i_womp;
  169.     for (s=i_womp; *s; s++) {
  170.     if (*s == '\n')
  171.         i_ptr[++iline] = s+1;    /* these are NOT null terminated */
  172.     }
  173.     input_lines = iline - 1;
  174.  
  175.     /* now check for revision, if any */
  176.  
  177.     if (revision != Nullch) { 
  178.     if (!rev_in_string(i_womp)) {
  179.         if (force) {
  180.         if (verbose)
  181.             say2(
  182. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  183.             revision);
  184.         }
  185.         else {
  186.         ask2(
  187. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  188.             revision);
  189.         if (*buf != 'y')
  190.         fatal1("Aborted.\n");
  191.         }
  192.     }
  193.     else if (verbose)
  194.         say2("Good.  This file appears to be the %s version.\n",
  195.         revision);
  196.     }
  197.     return TRUE;            /* plan a will work */
  198. }
  199.  
  200. /* Keep (virtually) nothing in memory. */
  201.  
  202. void
  203. plan_b(filename)
  204. char *filename;
  205. {
  206.     Reg3 FILE *ifp;
  207.     Reg1 int i = 0;
  208.     Reg2 int maxlen = 1;
  209.     Reg4 bool found_revision = (revision == Nullch);
  210.  
  211.     using_plan_a = FALSE;
  212.     if ((ifp = fopen(filename, "r")) == Nullfp)
  213.     fatal2("Can't open file %s\n", filename);
  214.     if ((tifd = creat(TMPINNAME, 0666)) < 0)
  215.     fatal2("Can't open file %s\n", TMPINNAME);
  216.     while (fgets(buf, sizeof buf, ifp) != Nullch) {
  217.     if (revision != Nullch && !found_revision && rev_in_string(buf))
  218.         found_revision = TRUE;
  219.     if ((i = strlen(buf)) > maxlen)
  220.         maxlen = i;            /* find longest line */
  221.     }
  222.     if (revision != Nullch) {
  223.     if (!found_revision) {
  224.         if (force) {
  225.         if (verbose)
  226.             say2(
  227. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  228.             revision);
  229.         }
  230.         else {
  231.         ask2(
  232. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  233.             revision);
  234.         if (*buf != 'y')
  235.             fatal1("Aborted.\n");
  236.         }
  237.     }
  238.     else if (verbose)
  239.         say2("Good.  This file appears to be the %s version.\n",
  240.         revision);
  241.     }
  242.     Fseek(ifp, 0L, 0);        /* rewind file */
  243.     lines_per_buf = BUFFERSIZE / maxlen;
  244.     tireclen = maxlen;
  245.     tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
  246.     tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
  247.     if (tibuf[1] == Nullch)
  248.     fatal1("Can't seem to get enough memory.\n");
  249.     for (i=1; ; i++) {
  250.     if (! (i % lines_per_buf))    /* new block */
  251.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  252.         fatal1("patch: can't write temp file.\n");
  253.     if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
  254.       == Nullch) {
  255.         input_lines = i - 1;
  256.         if (i % lines_per_buf)
  257.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  258.             fatal1("patch: can't write temp file.\n");
  259.         break;
  260.     }
  261.     }
  262.     Fclose(ifp);
  263.     Close(tifd);
  264.     if ((tifd = open(TMPINNAME, 0)) < 0) {
  265.     fatal2("Can't reopen file %s\n", TMPINNAME);
  266.     }
  267. }
  268.  
  269. /* Fetch a line from the input file, \n terminated, not necessarily \0. */
  270.  
  271. char *
  272. ifetch(line,whichbuf)
  273. Reg1 LINENUM line;
  274. int whichbuf;                /* ignored when file in memory */
  275. {
  276.     if (line < 1 || line > input_lines)
  277.     return "";
  278.     if (using_plan_a)
  279.     return i_ptr[line];
  280.     else {
  281.     LINENUM offline = line % lines_per_buf;
  282.     LINENUM baseline = line - offline;
  283.  
  284.     if (tiline[0] == baseline)
  285.         whichbuf = 0;
  286.     else if (tiline[1] == baseline)
  287.         whichbuf = 1;
  288.     else {
  289.         tiline[whichbuf] = baseline;
  290. #ifndef lint        /* complains of long accuracy */
  291.         Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
  292. #endif
  293.         if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
  294.         fatal2("Error reading tmp file %s.\n", TMPINNAME);
  295.     }
  296.     return tibuf[whichbuf] + (tireclen*offline);
  297.     }
  298. }
  299.  
  300. /* True if the string argument contains the revision number we want. */
  301.  
  302. bool
  303. rev_in_string(string)
  304. char *string;
  305. {
  306.     Reg1 char *s;
  307.     Reg2 int patlen;
  308.  
  309.     if (revision == Nullch)
  310.     return TRUE;
  311.     patlen = strlen(revision);
  312.     if (strnEQ(string,revision,patlen) && isspace(string[patlen]))
  313.     return TRUE;
  314.     for (s = string; *s; s++) {
  315.     if (isspace(*s) && strnEQ(s+1, revision, patlen) && 
  316.         isspace(s[patlen+1] )) {
  317.         return TRUE;
  318.     }
  319.     }
  320.     return FALSE;
  321. }
  322.  
  323.